package com.barbeque.mall.controller.mallmanagement;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.issue.MarketIssue;
import com.barbeque.mall.bean.order.MarketOrder;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.service.mallmanagement.IssueService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ycc
 * @date 2022/07/15 19:58:00
 */
@RestController
@RequestMapping("admin/issue")
public class IssueController {

    @Autowired
    IssueService issueService;

    //GET http://localhost:8083/admin/issue/list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissions("admin:issue:list")
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo pageInfo,MarketIssue issue){
        //TODO:查询问题信息
        CommonData<MarketOrder> list = issueService.list(pageInfo,issue);

        return BaseRespVo.ok(list);
    }

    //POST http://localhost:8083/admin/issue/update
    @RequiresPermissions("admin:issue:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketIssue issue){
        int affectedRows = issueService.update(issue);
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(issue);
    }

    //POST http://localhost:8083/admin/issue/delete
    @RequiresPermissions("admin:issue:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketIssue issue){
        int affectedRows = issueService.delete(issue);
        //未删除成功提示错误信息
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok("ok");
    }

    //POST http://localhost:8083/admin/issue/create
    @RequiresPermissions("admin:issue:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketIssue issue){
        if (StringUtils.isEmpty(issue.getAnswer()) || StringUtils.isEmpty(issue.getQuestion())) {
            return BaseRespVo.invalidData("内容不能为空");
        }
        int affectedRows = issueService.create(issue);
        //未添加成功提示错误信息
        if (affectedRows==0) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(issue);
    }
}
